<template>
    <div>
        <div class="pd-10">
            <el-table :data="tableData" border stripe :header-cell-class-name="headerBg">
                <el-table-column type="index" width="50" />
                <el-table-column prop="topicTitle" label="题目" width="300" />
                <el-table-column prop="topicAnswer" label="答案" width="300" />
                <el-table-column prop="" label="查重" width="110">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" @click="duplicateCheck(scope.row)" round>查重</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="创建人" width="100">
                    <template slot-scope="scope">
                        <el-button type="text" @click="getUser(scope.row)" round>
                            {{ scope.row.sysUser.name }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="topicType.name" label="类型" />
                <el-table-column prop="topicComplexity" label="难度系数" width="100" />
                <el-table-column prop="createTime" label="创建时间" width="150" />
            </el-table>
        </div>

        <div style="padding: 10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :page-sizes="[5, 10, 20, 30]" :current-page="pageNum" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <el-dialog customClass="customClass" :visible.sync="dialogTableVisible">
            <p style="font-weight: bold;">{{ topicData.topicTitle }}</p>
            <el-form :model="formKeyword" ref="formKeyword" label-width="100px" class="demo-ruleForm">
                <el-form-item>
                    <p>类型：<el-input placeholder="请输入内容" v-model="topicType.name" style="width: 70px;" disabled>
                        </el-input></p>
                </el-form-item>
                <el-form-item prop="keyword" :rules="[
                    { required: true, message: '关键字不能为空', trigger: 'blur' },]">
                    <el-input v-model="formKeyword.keyword" placeholder="请输入关键字" style="width: 200px;"></el-input>
                    <el-button class="ml-5" type="success" icon="el-icon-search" @click="loadTopic('formKeyword')"
                        :loading="loadStatus">搜索</el-button>
                </el-form-item>

            </el-form>
            <el-table :data="likenessData">
                <el-table-column property="title" label="类似题目" />
            </el-table>
            <el-button type="primary" circle @click="approve(topicData.id)" :disabled="checkStatus">通过</el-button>
            <el-button type="danger" circle @click="refuse(topicData.id)">拒绝</el-button>
        </el-dialog>
        <el-dialog title="创建人信息" :visible.sync="dialogFormVisible">
            <el-form :model="formDialog">
                <el-form-item label="姓名:" :label-width="formLabelWidth">
                    <p>{{ formDialog.name }}</p>
                </el-form-item>
                <el-form-item label="电话:" :label-width="formLabelWidth">
                    <p>{{ formDialog.mobile }}</p>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>

import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import WangEditor from '@/components/wangEditor/WangEditor';
export default {
    components: { Editor, Toolbar, WangEditor },
    name: "TopicType",
    data() {
        return {
            tableData: [],
            pageSize: 10,
            pageNum: 1,
            total: 0,
            headerBg: "headerBg",
            formLabelWidth: "90px",
            dialogTableVisible: false,
            dialogFormVisible: false,
            topicType: {},
            topicData: {},
            likenessData: [],
            designation: "",
            formDialog: {},
            loadStatus: false,
            keyword: "",
            checkStatus: false,
            formKeyword: {
                keyword: "",
            },
            rules: {
                keyword: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                ]
            }
        }
    },
    created() {
        this.load();
    },

    methods: {
        duplicateCheck(row) {
            this.likenessData = [];
            this.topicData = row;
            this.dialogTableVisible = true;
            this.topicType = row.topicType;
            this.request.get("/topic_audit/check", {
                params: {
                    keyword: row.topicTitle,
                    type: row.topicType.name
                }
            }).then(res => {
                this.likenessData = res.data;
                if (this.likenessData.length > 0) {
                    this.checkStatus = true;
                } else {
                    this.checkStatus = false;
                }
            })

        },
        approve(id) {
            this.request.post("topic/approve/" + id).then(res => {
                this.dialogTableVisible = false;
            })
        },
        refuse(id) {
            this.request.post("topic/refuse/" + id).then(res => {
                this.dialogTableVisible = false;
            })
        },
        getUser(row) {
            this.dialogFormVisible = true;
            this.formDialog = row.sysUser;

        },
        loadTopic(forName) {
            this.$refs[forName].validate((valid) => {
                if (valid) {
                    this.loadStatus = true;
                    this.request.get("/topic_audit/check", {
                        params: {
                            keyword: this.formKeyword.keyword,
                            type: this.topicType.name
                        }
                    }).then(res => {
                        this.likenessData = res.data
                        this.loadStatus = false
                    })
                }
            });

        },
        load() {
            this.request.get("/topic_audit/listPage", {
                params: {
                    pageNumber: this.pageNum,
                    pageSize: this.pageSize,
                }
            }).then(res => {
                this.tableData = res.data.list;
                this.total = res.data.total;
            });
        },
        deleteTopticType(id) {
            this.$confirm("此操作将永久删除该, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.request.delete("/topic_java/delete/" + id).then(res => {
                    if (res.code == 200) {
                        this.$message.success("删除成功");
                        this.load();
                    }
                    else {
                        this.$message.error(res.msg);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消删除"
                });
                this.load();
            });
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {
            this.pageNum = pageNum;
            this.load();
        },
    },
    mounted() {
        setTimeout(() => {
            this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
        }, 1500)
    },
    watch: {
        dialogTableVisible: {
            handler(value) {
                if (!value) {
                    this.load()
                }
            }
        }
    },
}
</script>
<style>
.headerBg {
    background: #eee !important;
}

.customClass {
    width: 67% !important;
    min-width: 100px !important;
}
</style>